<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>String Generators (string, lit)</title>
<link rel="stylesheet" href="../../../../../../../../doc/src/boostbook.css" type="text/css">
<meta name="generator" content="DocBook XSL Stylesheets V1.79.1">
<link rel="home" href="../../../../index.html" title="Spirit 2.59">
<link rel="up" href="../string.html" title="String Generators">
<link rel="prev" href="../string.html" title="String Generators">
<link rel="next" href="symbols.html" title="Symbols Generator (symbols)">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<table cellpadding="2" width="100%"><tr>
<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../../../../boost.png"></td>
<td align="center"><a href="../../../../../../../../index.html">Home</a></td>
<td align="center"><a href="../../../../../../../../libs/libraries.htm">Libraries</a></td>
<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td>
<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
<td align="center"><a href="../../../../../../../../more/index.htm">More</a></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="../string.html"><img src="../../../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../string.html"><img src="../../../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../../../index.html"><img src="../../../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="symbols.html"><img src="../../../../../../../../doc/src/images/next.png" alt="Next"></a>
</div>
<div class="section">
<div class="titlepage"><div><div><h5 class="title">
<a name="spirit.karma.reference.string.string"></a><a class="link" href="string.html" title="String Generators (string, lit)">String Generators
          (<code class="computeroutput"><span class="identifier">string</span></code>, <code class="computeroutput"><span class="identifier">lit</span></code>)</a>
</h5></div></div></div>
<h6>
<a name="spirit.karma.reference.string.string.h0"></a>
            <span class="phrase"><a name="spirit.karma.reference.string.string.description"></a></span><a class="link" href="string.html#spirit.karma.reference.string.string.description">Description</a>
          </h6>
<p>
            The string generators described in this section are:
          </p>
<p>
            The <code class="computeroutput"><span class="identifier">string</span></code> generator
            emits a string of characters. The <code class="computeroutput"><span class="identifier">string</span></code>
            generator is implicitly verbatim: the <code class="computeroutput"><span class="identifier">delimit</span></code>
            parser is not applied in between characters of the string. The <code class="computeroutput"><span class="identifier">string</span></code> generator has an associated
            <a class="link" href="../basics.html#spirit.karma.reference.basics.character_encoding_namespace">Character
            Encoding Namespace</a>. This is needed when doing basic operations
            such as forcing lower or upper case. Examples:
          </p>
<pre class="programlisting"><span class="identifier">string</span><span class="special">(</span><span class="string">"Hello"</span><span class="special">)</span>
<span class="identifier">string</span><span class="special">(</span><span class="identifier">L</span><span class="string">"Hello"</span><span class="special">)</span>
<span class="identifier">string</span><span class="special">(</span><span class="identifier">s</span><span class="special">)</span>         <span class="comment">// s is a std::string</span>
</pre>
<p>
            <code class="computeroutput"><span class="identifier">lit</span></code>, like <code class="computeroutput"><span class="identifier">string</span></code>, also emits a string of characters.
            The main difference is that <code class="computeroutput"><span class="identifier">lit</span></code>
            does not consumes an attribute. A plain string like <code class="computeroutput"><span class="string">"hello"</span></code>
            or a <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">basic_string</span></code> is equivalent to a <code class="computeroutput"><span class="identifier">lit</span></code>. Examples:
          </p>
<pre class="programlisting"><span class="string">"Hello"</span>
<span class="identifier">lit</span><span class="special">(</span><span class="string">"Hello"</span><span class="special">)</span>
<span class="identifier">lit</span><span class="special">(</span><span class="identifier">L</span><span class="string">"Hello"</span><span class="special">)</span>
<span class="identifier">lit</span><span class="special">(</span><span class="identifier">s</span><span class="special">)</span>            <span class="comment">// s is a std::string</span>
</pre>
<h6>
<a name="spirit.karma.reference.string.string.h1"></a>
            <span class="phrase"><a name="spirit.karma.reference.string.string.header"></a></span><a class="link" href="string.html#spirit.karma.reference.string.string.header">Header</a>
          </h6>
<pre class="programlisting"><span class="comment">// forwards to &lt;boost/spirit/home/karma/string/lit.hpp&gt;</span>
<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">spirit</span><span class="special">/</span><span class="identifier">include</span><span class="special">/</span><span class="identifier">karma_string</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
</pre>
<p>
            Also, see <a class="link" href="../../../structure/include.html" title="Include">Include Structure</a>.
          </p>
<h6>
<a name="spirit.karma.reference.string.string.h2"></a>
            <span class="phrase"><a name="spirit.karma.reference.string.string.namespace"></a></span><a class="link" href="string.html#spirit.karma.reference.string.string.namespace">Namespace</a>
          </h6>
<div class="informaltable"><table class="table">
<colgroup><col></colgroup>
<thead><tr><th>
                    <p>
                      Name
                    </p>
                  </th></tr></thead>
<tbody>
<tr><td>
                    <p>
                      <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">spirit</span><span class="special">::</span><span class="identifier">lit</span> <span class="comment">// alias:
                      boost::spirit::karma::lit</span></code>
                    </p>
                  </td></tr>
<tr><td>
                    <p>
                      <code class="computeroutput"><span class="identifier">ns</span><span class="special">::</span><span class="identifier">string</span></code>
                    </p>
                  </td></tr>
</tbody>
</table></div>
<p>
            In the table above, <code class="computeroutput"><span class="identifier">ns</span></code>
            represents a <a class="link" href="../basics.html#spirit.karma.reference.basics.character_encoding_namespace">Character
            Encoding Namespace</a> used by the corresponding string generator.
          </p>
<h6>
<a name="spirit.karma.reference.string.string.h3"></a>
            <span class="phrase"><a name="spirit.karma.reference.string.string.model_of"></a></span><a class="link" href="string.html#spirit.karma.reference.string.string.model_of">Model
            of</a>
          </h6>
<div class="blockquote"><blockquote class="blockquote"><p>
              <a class="link" href="../generator_concepts/primitivegenerator.html" title="PrimitiveGenerator"><code class="computeroutput"><span class="identifier">PrimitiveGenerator</span></code></a>
            </p></blockquote></div>
<div class="variablelist">
<p class="title"><b>Notation</b></p>
<dl class="variablelist">
<dt><span class="term"><code class="computeroutput"><span class="identifier">s</span></code></span></dt>
<dd><p>
                  Character-class specific string (See Character Class Types), or
                  a <a class="link" href="../basics.html#spirit.karma.reference.basics.lazy_argument">Lazy
                  Argument</a> that evaluates to a character-class specific string
                  value
                </p></dd>
<dt><span class="term"><code class="computeroutput"><span class="identifier">S</span></code></span></dt>
<dd><p>
                  The type of a character-class specific string <code class="computeroutput"><span class="identifier">s</span></code>.
                </p></dd>
<dt><span class="term"><code class="computeroutput"><span class="identifier">ns</span></code></span></dt>
<dd><p>
                  A <a class="link" href="../basics.html#spirit.karma.reference.basics.character_encoding_namespace">Character
                  Encoding Namespace</a>.
                </p></dd>
</dl>
</div>
<h6>
<a name="spirit.karma.reference.string.string.h4"></a>
            <span class="phrase"><a name="spirit.karma.reference.string.string.expression_semantics"></a></span><a class="link" href="string.html#spirit.karma.reference.string.string.expression_semantics">Expression
            Semantics</a>
          </h6>
<p>
            Semantics of an expression is defined only where it differs from, or
            is not defined in <a class="link" href="../generator_concepts/primitivegenerator.html" title="PrimitiveGenerator"><code class="computeroutput"><span class="identifier">PrimitiveGenerator</span></code></a>.
          </p>
<div class="informaltable"><table class="table">
<colgroup>
<col>
<col>
</colgroup>
<thead><tr>
<th>
                    <p>
                      Expression
                    </p>
                  </th>
<th>
                    <p>
                      Description
                    </p>
                  </th>
</tr></thead>
<tbody>
<tr>
<td>
                    <p>
                      <code class="computeroutput"><span class="identifier">s</span></code>
                    </p>
                  </td>
<td>
                    <p>
                      Generate the string literal <code class="computeroutput"><span class="identifier">s</span></code>.
                      This generator never fails (unless the underlying output stream
                      reports an error).
                    </p>
                  </td>
</tr>
<tr>
<td>
                    <p>
                      <code class="computeroutput"><span class="identifier">lit</span><span class="special">(</span><span class="identifier">s</span><span class="special">)</span></code>
                    </p>
                  </td>
<td>
                    <p>
                      Generate the string literal <code class="computeroutput"><span class="identifier">s</span></code>.
                      This generator never fails (unless the underlying output stream
                      reports an error).
                    </p>
                  </td>
</tr>
<tr>
<td>
                    <p>
                      <code class="computeroutput"><span class="identifier">ns</span><span class="special">::</span><span class="identifier">string</span></code>
                    </p>
                  </td>
<td>
                    <p>
                      Generate the string provided by a mandatory attribute interpreted
                      in the character set defined by <code class="computeroutput"><span class="identifier">ns</span></code>.
                      This generator never fails (unless the underlying output stream
                      reports an error).
                    </p>
                  </td>
</tr>
<tr>
<td>
                    <p>
                      <code class="computeroutput"><span class="identifier">ns</span><span class="special">::</span><span class="identifier">string</span><span class="special">(</span><span class="identifier">s</span><span class="special">)</span></code>
                    </p>
                  </td>
<td>
                    <p>
                      Generate the string <code class="computeroutput"><span class="identifier">s</span></code>
                      as provided by the immediate literal value the generator is
                      initialized from. If this generator has an associated attribute
                      it succeeds only if the attribute is equal to the immediate
                      literal (unless the underlying output stream reports an error).
                      Otherwise this generator fails and does not generate any output.
                    </p>
                  </td>
</tr>
</tbody>
</table></div>
<div class="note"><table border="0" summary="Note">
<tr>
<td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="../../../../images/note.png"></td>
<th align="left">Note</th>
</tr>
<tr><td align="left" valign="top"><p>
              The generators <code class="computeroutput"><span class="identifier">lit</span><span class="special">(</span><span class="identifier">s</span><span class="special">)</span></code> and <code class="computeroutput"><span class="identifier">string</span><span class="special">(</span><span class="identifier">s</span><span class="special">)</span></code> can be initialized either using a
              string literal value (i.e. <code class="computeroutput"><span class="string">"abc"</span></code>),
              or using a <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">basic_string</span><span class="special">&lt;</span><span class="identifier">char_type</span><span class="special">,</span>
              <span class="special">...&gt;</span></code>, where <code class="computeroutput"><span class="identifier">char_type</span></code> is the required value type
              of the underlying character sequence.
            </p></td></tr>
</table></div>
<div class="caution"><table border="0" summary="Caution">
<tr>
<td rowspan="2" align="center" valign="top" width="25"><img alt="[Caution]" src="../../../../images/caution.png"></td>
<th align="left">Caution</th>
</tr>
<tr><td align="left" valign="top"><p>
              The generator <code class="computeroutput"><span class="identifier">string</span><span class="special">(</span><span class="identifier">s</span><span class="special">)</span></code> up to version 2.4.1 of Spirit has
              an undocumented feature. Given argument <code class="computeroutput"><span class="identifier">s</span></code>
              generator succeeds as long as <code class="computeroutput"><span class="identifier">s</span></code>
              is a prefix of given attribute. This problem has been fixed in Spirit
              V2.4.2.
            </p></td></tr>
</table></div>
<h6>
<a name="spirit.karma.reference.string.string.h5"></a>
            <span class="phrase"><a name="spirit.karma.reference.string.string.attributes"></a></span><a class="link" href="string.html#spirit.karma.reference.string.string.attributes">Attributes</a>
          </h6>
<div class="informaltable"><table class="table">
<colgroup>
<col>
<col>
</colgroup>
<thead><tr>
<th>
                    <p>
                      Expression
                    </p>
                  </th>
<th>
                    <p>
                      Attribute
                    </p>
                  </th>
</tr></thead>
<tbody>
<tr>
<td>
                    <p>
                      <code class="computeroutput"><span class="identifier">s</span></code>
                    </p>
                  </td>
<td>
                    <p>
                      <code class="computeroutput"><span class="identifier">unused</span></code>
                    </p>
                  </td>
</tr>
<tr>
<td>
                    <p>
                      <code class="computeroutput"><span class="identifier">lit</span><span class="special">(</span><span class="identifier">s</span><span class="special">)</span></code>
                    </p>
                  </td>
<td>
                    <p>
                      <code class="computeroutput"><span class="identifier">unused</span></code>
                    </p>
                  </td>
</tr>
<tr>
<td>
                    <p>
                      <code class="computeroutput"><span class="identifier">ns</span><span class="special">::</span><span class="identifier">string</span></code>
                    </p>
                  </td>
<td>
                    <p>
                      <code class="computeroutput"><span class="identifier">S</span></code>, attribute
                      is mandatory (otherwise compilation will fail)
                    </p>
                  </td>
</tr>
<tr>
<td>
                    <p>
                      <code class="computeroutput"><span class="identifier">ns</span><span class="special">::</span><span class="identifier">string</span><span class="special">(</span><span class="identifier">s</span><span class="special">)</span></code>
                    </p>
                  </td>
<td>
                    <p>
                      <code class="computeroutput"><span class="identifier">S</span></code>, attribute
                      is optional, if it is supplied, the generator compares the
                      attribute with <code class="computeroutput"><span class="identifier">s</span></code>
                      and succeeds only if both are equal, failing otherwise
                    </p>
                  </td>
</tr>
</tbody>
</table></div>
<div class="note"><table border="0" summary="Note">
<tr>
<td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="../../../../images/note.png"></td>
<th align="left">Note</th>
</tr>
<tr><td align="left" valign="top"><p>
              In addition to their usual attribute of type <code class="computeroutput"><span class="identifier">S</span></code>
              all listed generators accept an instance of a <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">optional</span><span class="special">&lt;</span><span class="identifier">S</span><span class="special">&gt;</span></code> as well. If the <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">optional</span><span class="special">&lt;&gt;</span></code> is initialized (holds a value)
              the generators behave as if their attribute was an instance of <code class="computeroutput"><span class="identifier">S</span></code> and emit the value stored in the
              <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">optional</span><span class="special">&lt;&gt;</span></code>.
              Otherwise the generators will fail.
            </p></td></tr>
</table></div>
<h6>
<a name="spirit.karma.reference.string.string.h6"></a>
            <span class="phrase"><a name="spirit.karma.reference.string.string.complexity"></a></span><a class="link" href="string.html#spirit.karma.reference.string.string.complexity">Complexity</a>
          </h6>
<div class="blockquote"><blockquote class="blockquote"><p>
              O(N), where N is the number of characters emitted by the string generator
            </p></blockquote></div>
<h6>
<a name="spirit.karma.reference.string.string.h7"></a>
            <span class="phrase"><a name="spirit.karma.reference.string.string.example"></a></span><a class="link" href="string.html#spirit.karma.reference.string.string.example">Example</a>
          </h6>
<div class="note"><table border="0" summary="Note">
<tr>
<td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="../../../../images/note.png"></td>
<th align="left">Note</th>
</tr>
<tr><td align="left" valign="top"><p>
              The test harness for the example(s) below is presented in the <a class="link" href="../basics.html#spirit.karma.reference.basics.examples">Basics Examples</a>
              section.
            </p></td></tr>
</table></div>
<p>
            Some includes:
          </p>
<p>
</p>
<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">spirit</span><span class="special">/</span><span class="identifier">include</span><span class="special">/</span><span class="identifier">karma</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">spirit</span><span class="special">/</span><span class="identifier">include</span><span class="special">/</span><span class="identifier">support_utree</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">phoenix</span><span class="special">/</span><span class="identifier">core</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">phoenix</span><span class="special">/</span><span class="keyword">operator</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">fusion</span><span class="special">/</span><span class="identifier">include</span><span class="special">/</span><span class="identifier">std_pair</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">proto</span><span class="special">/</span><span class="identifier">deep_copy</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">iostream</span><span class="special">&gt;</span>
<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">string</span><span class="special">&gt;</span>
</pre>
<p>
          </p>
<p>
            Some using declarations:
          </p>
<p>
</p>
<pre class="programlisting"><span class="keyword">using</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">spirit</span><span class="special">::</span><span class="identifier">karma</span><span class="special">::</span><span class="identifier">lit</span><span class="special">;</span>
<span class="keyword">using</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">spirit</span><span class="special">::</span><span class="identifier">ascii</span><span class="special">::</span><span class="identifier">string</span><span class="special">;</span>
</pre>
<p>
          </p>
<p>
            Basic usage of <code class="computeroutput"><span class="identifier">string</span></code>
            generators:
          </p>
<p>
</p>
<pre class="programlisting"><span class="identifier">test_generator</span><span class="special">(</span><span class="string">"abc"</span><span class="special">,</span> <span class="string">"abc"</span><span class="special">);</span>
<span class="identifier">test_generator</span><span class="special">(</span><span class="string">"abc"</span><span class="special">,</span> <span class="identifier">lit</span><span class="special">(</span><span class="string">"abc"</span><span class="special">));</span>
<span class="identifier">test_generator</span><span class="special">(</span><span class="string">"abc"</span><span class="special">,</span> <span class="identifier">lit</span><span class="special">(</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span><span class="special">(</span><span class="string">"abc"</span><span class="special">)));</span>

<span class="identifier">test_generator_attr</span><span class="special">(</span><span class="string">"abc"</span><span class="special">,</span> <span class="identifier">string</span><span class="special">,</span> <span class="string">"abc"</span><span class="special">);</span>
<span class="identifier">test_generator</span><span class="special">(</span><span class="string">"abc"</span><span class="special">,</span> <span class="identifier">string</span><span class="special">(</span><span class="string">"abc"</span><span class="special">));</span>
<span class="identifier">test_generator</span><span class="special">(</span><span class="string">"abc"</span><span class="special">,</span> <span class="identifier">string</span><span class="special">(</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span><span class="special">(</span><span class="string">"abc"</span><span class="special">)));</span>

<span class="identifier">test_generator_attr</span><span class="special">(</span><span class="string">"abc"</span><span class="special">,</span> <span class="identifier">string</span><span class="special">(</span><span class="string">"abc"</span><span class="special">),</span> <span class="string">"abc"</span><span class="special">);</span>
<span class="identifier">test_generator_attr</span><span class="special">(</span><span class="string">""</span><span class="special">,</span> <span class="identifier">string</span><span class="special">(</span><span class="string">"abc"</span><span class="special">),</span> <span class="string">"cba"</span><span class="special">);</span>     <span class="comment">// fails (as "abc" != "cba")</span>
</pre>
<p>
          </p>
</div>
<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
<td align="left"></td>
<td align="right"><div class="copyright-footer">Copyright © 2001-2011 Joel de Guzman, Hartmut Kaiser<p>
        Distributed under the Boost Software License, Version 1.0. (See accompanying
        file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>)
      </p>
</div></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="../string.html"><img src="../../../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../string.html"><img src="../../../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../../../index.html"><img src="../../../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="symbols.html"><img src="../../../../../../../../doc/src/images/next.png" alt="Next"></a>
</div>
</body>
</html>
